package com.raining.inningz.ui.home;


import java.util.List;

import com.raining.inningz.R;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.manager.SetupFragments;
import com.raining.inningz.ui.play.PlayFragment;

import android.app.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;

import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class HomePageActivity extends FragmentActivity implements OnClickListener ,OnBackStackChangedListener{
	
	
	private Activity activity;
	
	private RelativeLayout relativeLayoutContent;
	
	private Button  btnPlay;
	private Button  btnViewScoreBoard;
	private Button  btnAbout;
	
	SetupFragments currentFragment;
	
	
	private AboutFragment aboutFragment;
	private PlayFragment playFragment;
	private ViewScoreFragment viewScoreFragment; 
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		
		activity = this;
		setContentView(R.layout.activity_hometab);
		
		intializeUI();
		setListner();
	}

	

	private void intializeUI() {
		
		relativeLayoutContent=(RelativeLayout)findViewById(R.id.relative_content);
		
		btnPlay=(Button)findViewById(R.id.btnPlay);
		btnViewScoreBoard=(Button)findViewById(R.id.btnViewScoreBoard);
		btnAbout=(Button)findViewById(R.id.btnAbout);
		
		
		playFragment = new PlayFragment();

		
		changeFragment(playFragment, IFragmentNameConstants.PLAY);
		
		currentFragment = SetupFragments.PLAY;
		setButtonBackgrounds();
		
	}
	
	private void setButtonBackgrounds() {
		
		btnPlay.setTextColor(Color.GRAY);
		btnViewScoreBoard.setTextColor(Color.GRAY);
		btnAbout.setTextColor(Color.GRAY);
		
		if(currentFragment==SetupFragments.PLAY){
			
			btnPlay.setTextColor(getResources().getColor(R.color.light_blue));
			btnPlay.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.play_press), null, null);
			
			btnViewScoreBoard.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.view_score), null, null);
			btnAbout.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.about), null, null);
			
			
		}
		else if(currentFragment==SetupFragments.VIEWSCORE){
			
			btnViewScoreBoard.setTextColor(getResources().getColor(R.color.light_blue));
			
			btnPlay.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.play), null, null);
			
			btnViewScoreBoard.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.view_score_press), null, null);
			btnAbout.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.about), null, null);
			
		}
		else if(currentFragment==SetupFragments.ABOUT){
			
			btnAbout.setTextColor(getResources().getColor(R.color.light_blue));
			
			btnPlay.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.play), null, null);
			
			btnViewScoreBoard.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.view_score), null, null);
			btnAbout.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.about_press), null, null);
		}
		
	}



	private void setListner() {
		btnPlay.setOnClickListener(this);
		btnViewScoreBoard.setOnClickListener(this);
		btnAbout.setOnClickListener(this);
		
		getSupportFragmentManager().addOnBackStackChangedListener(this);
		
		
	}

	@Override
	public void onClick(View v) {
		if(v==btnPlay){
			
			playFragment = new PlayFragment();
			changeFragment(playFragment,IFragmentNameConstants.PLAY);
			currentFragment = SetupFragments.PLAY;
			setButtonBackgrounds();
			
		}
		else if(v== btnViewScoreBoard){
			viewScoreFragment=new ViewScoreFragment();
			changeFragment(viewScoreFragment,IFragmentNameConstants.VIEW_SCORE_CARD);
			currentFragment = SetupFragments.VIEWSCORE;
			setButtonBackgrounds();
		}
		else if (v==btnAbout) {
			aboutFragment=new AboutFragment();
			changeFragment(aboutFragment,IFragmentNameConstants.ABOUT);
			currentFragment = SetupFragments.ABOUT;
			setButtonBackgrounds();
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private void changeFragment(Fragment newFragment,String fragmentName) {
		
		
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		
		transaction.replace(R.id.relative_content, newFragment,fragmentName);
		transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		transaction.addToBackStack(fragmentName);
		//transaction.commitAllowingStateLoss();
		
		transaction.commit();
		
		
	}



	@Override
	public void onBackStackChanged() {
		 FragmentManager localFragmentManager = getSupportFragmentManager();
		
		int backStackEntryCount = localFragmentManager.getBackStackEntryCount();
		
		
		
		
		
		List<Fragment> fragments=localFragmentManager.getFragments();
		
		
		
		if(backStackEntryCount>0)
		{
		for (int i = 0; i < fragments.size(); i++) {
			
			System.out.println("Fag "+(i+1)+" "+fragments.get(i).getTag());
			
		}
		}
		else if(backStackEntryCount==0){
			finish();
		}
		
	}

}
